package com.taptech.doufu.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RatingBar;
import android.widget.TextView;

import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.util.TextUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.NovelBean;
import com.taptech.doufu.bean.SweepBean;
import com.taptech.doufu.bean.SweepFlowerDesBean;
import com.taptech.doufu.bean.UserBean;
import com.taptech.doufu.bean.CommentDataBean;
import com.taptech.doufu.ui.adapter.SweepReplyAdapter;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.bean.personalcenter.SweepFlowerBean;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.ui.view.personalcenter.ContentTagsViewGroup;
import com.taptech.doufu.services.HomeMainServices;
import com.taptech.doufu.services.SweepFlowerService;
import com.taptech.doufu.ui.adapter.HorizontalListViewAdapter;
import com.taptech.doufu.ui.view.HorizontalListView;
import com.taptech.doufu.util.at.AtClickListen;
import com.taptech.doufu.util.at.AtUtil;
import com.taptech.doufu.util.AnimationUtil;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.PullToRefreshListView;
import com.taptech.doufu.ui.view.PullToRefreshListView.OnScrollToBottomListener;
import com.taptech.doufu.ui.view.RoundImageView;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class SweepFlowerActivity extends DiaobaoBaseActivity implements HttpResponseListener {

    private static final int REFRESH_FLOWER_STATUS = -4;
    private static final int RESET_CLICK_FLOWER = -3;
    private static final int SEND_FLOWER_FAIL = -2;
    private static final int SEND_FLOWER_SUCCESS = -1;
    private static final int SEND_FLOWER_TIME = 4;
    private RoundImageView flower, flowerIngUserImg;
    private ImageView[] flower_copy = new ImageView[8];
    // 个人用户信息
    private UserBean userBean;
    private Timer timer;
    boolean isFlowerClick = false;
    private TextView flowerStatusTips;
    private int count = 0;
    private int j = 8;
    private String mReviewId;// 文评id
    /**
     * 献花按钮是否可献花，献花完后有3秒的冷却时间
     */
    private boolean clickable = true;

    /**
     * 单篇文章献花总数
     */
    private TextView flower_sum;
    private int flower_count = 0;

    private HorizontalListView flowerUserList;
    private HorizontalListViewAdapter flowerUserAdapter;
    /**
     * 无人献花时的提示
     */
    private TextView no_flower;
    Animation flowerAnimation;

    TextView mTitleLevel;
    TextView sweepTitle;
    TextView sweepDes;

    TextView mUserName;
    RatingBar detailRatingBar;
    RoundImageView mUserImg;
    RoundImageView medalImg;

    private View header;
    private EditText commentMessage;
    private View sendBtn, flagGroup;
    private PullToRefreshListView list;
    private TextView startFlowerTips;
    private SweepReplyAdapter adapter;
    private int pageIndex;
    private String last = "";
    private String isAdministrator;
    private TextView tv_at;
    // 标签相关
    List<String> tagsDataList;

    private TextView mSweepCreateTime;

    ContentTagsViewGroup tagsView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sweep_flower_activity);
        initFlower();
        initTagsView();
    }


    @SuppressLint("SetJavaScriptEnabled")
    private void initFlower() {
        // list head
        header = getLayoutInflater().inflate(R.layout.sweep_flower_list_head, null);
        try {
            for (int i = 1; i < 9; i++) {
                Field fieldLayout = R.id.class.getDeclaredField("sweep_flower_copy_" + i);
                flower_copy[i - 1] = (ImageView) header.findViewById(Integer.parseInt(fieldLayout.get(null).toString()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        flower = (RoundImageView) header.findViewById(R.id.sweep_flower);
        no_flower = (TextView) header.findViewById(R.id.no_flower);
        flowerStatusTips = (TextView) header.findViewById(R.id.flower_ing_status);
        startFlowerTips = (TextView) header.findViewById(R.id.flower_ing_tips);
        flower_sum = (TextView) header.findViewById(R.id.flower_sum);
        flowerUserList = (HorizontalListView) header.findViewById(R.id.flower_rank_listview);
        mTitleLevel = (TextView) header.findViewById(R.id.flower_framelayout_title_level);
        sweepDes = (TextView) header.findViewById(R.id.flower_framelayout_sweep_des);

        mUserName = (TextView) header.findViewById(R.id.flower_framelayout_sweep_username);
        detailRatingBar = (RatingBar) header.findViewById(R.id.flower_framelayout_sweep_ratingbar);
        mUserImg = (RoundImageView) header.findViewById(R.id.flower_framelayout_sweep_userimg);
        medalImg = (RoundImageView) header.findViewById(R.id.flower_framelayout_sweep_userimg_medal);

        flowerIngUserImg = (RoundImageView) header.findViewById(R.id.flower_ing_userimg);
        flowerUserAdapter = new HorizontalListViewAdapter(SweepFlowerActivity.this);
        flowerUserList.setAdapter(flowerUserAdapter);
        flowerAnimation = AnimationUtils.loadAnimation(this, R.anim.sweep_flower_rotate);
        LinearInterpolator lin = new LinearInterpolator();
        flowerAnimation.setInterpolator(lin);
        mReviewId = getIntent().getStringExtra(Constant.SWEEP_ID);

        // list head 之外
        sweepTitle = (TextView) findViewById(R.id.flower_framelayout_title);
        commentMessage = (EditText) findViewById(R.id.contents_acticity_comment_text);
        flagGroup = findViewById(R.id.contents_acticity_flag_group);
        flagGroup.setVisibility(View.INVISIBLE);
        sendBtn = findViewById(R.id.contents_acticity_send_btn);
        sendBtn.setVisibility(View.VISIBLE);
        initAt();
        list = (PullToRefreshListView) findViewById(R.id.sweep_flower_comment_list);
        list.setRefreshable(false);
        list.setLoadmoreable(true);

        list.setOnScrollToBottomListener(new OnScrollToBottomListener() {

            @Override
            public void OnScrollToBottom() {
                MobclickAgent.onEvent(SweepFlowerActivity.this, "sweep-more-comment");
                SweepFlowerService.getInstance().loadSweepReplyList(mReviewId, pageIndex, last, SweepFlowerActivity.this);

            }
        });
        list.addHeaderView(header);
        adapter = new SweepReplyAdapter(this);
        list.setAdapter(adapter);
        // 献花用户
        SweepFlowerService.getInstance().loadSweepFlowerDes(mReviewId, this);
        SweepFlowerService.getInstance().loadSweepReplyList(mReviewId, 0, "", this);
        makeMorePopupWindow();
    }

    private void initAt() {
        tv_at = (TextView) findViewById(R.id.contents_acticity_at);
        AtClickListen atClickListen = new AtClickListen(SweepFlowerActivity.this, commentMessage, tv_at);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constant.AT_RESULI) {
            if (data != null) {
                String textString = "";
                String user_id = data.getStringExtra("user_id");
                String user_nikename = data.getStringExtra("user_nikename");
                if (user_nikename != null) {
                    if (requestCode == Constant.AT_CLICK) {
                        textString = commentMessage.getText().toString() + "@" + user_nikename + " ";
                    } else if (requestCode == Constant.AT_WRITE) {
                        textString = commentMessage.getText().toString() + user_nikename + " ";
                    }
                    new AtUtil(commentMessage, textString, Constant.AT_WRITE);
                }
            }
        }
    }

    public void showFlowerRule(View view) {
        Intent intent = new Intent();
        intent.setClass(this, BrowseActivity.class);
        intent.putExtra(Constant.URL, "http://www.doufu.la/static/flowers/rule.html");
        intent.putExtra(Constant.TITLE, "送花规则");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    public void publishComment(View view) {
        if (!AccountService.getInstance().isLogin()) {
            AccountService.getInstance().jumpToLogin();
            return;
        }
        MobclickAgent.onEvent(SweepFlowerActivity.this, "sweep-comment");
        if (commentMessage.getText() != null && !commentMessage.getText().toString().equals("")) {
            SweepFlowerService.getInstance().commitSweepReply(mReviewId, commentMessage.getText().toString(), SweepFlowerActivity.this);
        } else {
            UIUtil.toastMessage(SweepFlowerActivity.this, "评论不能为空哦");
        }
    }

    private void clearComment() {
        ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        commentMessage.setHint("我也说一句...");
        commentMessage.setText("");
    }

    int flowerSum = 0;

    @SuppressWarnings("unchecked")
    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        try {
            switch (handleType) {
                case SweepFlowerService.HANDLE_TYPE_LOAD_FLOWER_RANK:// 献花用户
                    final JSONObject jsonObject = (JSONObject) response.getData();
                    if (response.getStatus() == Constant.STATUS_SUCCESS) {
                        List<SweepFlowerBean> mainList;
                        if (jsonObject.get("list") instanceof JSONArray) {
                            mainList = new ArrayList<SweepFlowerBean>();
                            JSONArray mainArray = jsonObject.getJSONArray("list");
                            SweepFlowerBean contentBean = null;
                            for (int i = 0; i < mainArray.length(); i++) {
                                contentBean = new SweepFlowerBean();
                                if (mainArray.get(i) instanceof JSONObject) {
                                    contentBean.setJson(mainArray.getJSONObject(i));
                                }
                                mainList.add(contentBean);
                            }
                            if (mainList.size() != 0) {
                                no_flower.setVisibility(View.GONE);
                                flowerUserAdapter.setFlowerInfo(mainList);

                                int sum = 0;
                                for (int i = 0; i < mainList.size(); i++) {
                                    sum = sum + Integer.valueOf(mainList.get(i).getNum());
                                }
                                flowerSum = sum;
                                flower_sum.setText(flowerSum + "");
                            } else {
                                no_flower.setVisibility(View.VISIBLE);
                                flower_sum.setText("0");
                            }
                        }

                    }
                    break;
                case SweepFlowerService.SEND_FLOWER:// 献花
                    if (response.getStatus() == Constant.STATUS_SUCCESS) {
                        flowerHandler.sendEmptyMessage(SEND_FLOWER_SUCCESS);
                    } else {
                        Message mes = Message.obtain();
                        mes.obj = DiaobaoUtil.getErrorTips(response).trim();
                        mes.what = SEND_FLOWER_FAIL;
                        flowerHandler.sendMessage(mes);
                    }
                    break;
                case SweepFlowerService.HANDLE_TYPE_LOAD_SWEEP_REPLY:// 读取评论列表
                    List<CommentDataBean> mainList = null;
                    if (response.getStatus() == Constant.STATUS_SUCCESS) {
                        JSONObject sweepComObj = (JSONObject) response.getData();
                        if (sweepComObj.get(Constant.LIST) instanceof JSONArray) {
                            mainList = new ArrayList<CommentDataBean>();
                            JSONArray mainArray = sweepComObj.getJSONArray(Constant.LIST);
                            CommentDataBean contentBean = null;
                            for (int i = 0; i < mainArray.length(); i++) {
                                contentBean = new CommentDataBean();
                                if (mainArray.get(i) instanceof JSONObject) {
                                    contentBean.setJson(mainArray.getJSONObject(i));
                                }
                                mainList.add(contentBean);
                            }
                        }
                        last = sweepComObj.getString("last");
                        if (mainList.size() < Constant.PAGE_SIZE_20) {
                            list.setFull(true);
                        } else {
                            pageIndex++;
                        }
                        if (pageIndex == 0) {
                            adapter.setDataSource(mainList);
                        } else {
                            adapter.setSuperDatas(mainList);
                        }
                        list.loadMoreComplete();
                    } else {
                        UIUtil.toastMessage(this, DiaobaoUtil.getErrorTips(response));
                    }
                    break;
                case SweepFlowerService.HANDLE_TYPE_COMMIT_SWEEP_REPLY:// 发表评论
                    if (response.getStatus() == Constant.STATUS_SUCCESS) {
                        CommentDataBean data = new CommentDataBean();
                        data.setJson(((JSONObject) response.getData()).getJSONObject(Constant.COMMENT));
                        data.getUser().setName(AccountService.getInstance().getBaseAccount().getNickname());
                        data.getUser().setIcon(AccountService.getInstance().getBaseAccount().getUser_head_img());
                        adapter.addRefreshData(data);
                        clearComment();
                    } else {
                        UIUtil.toastMessage(SweepFlowerActivity.this, DiaobaoUtil.getErrorTips(response));
                    }
                    break;
                // 是否已经收藏
                case SweepFlowerService.HANDLE_TYPE_HAS_COLLECT_SWEEP:
                    if (response.getStatus() == Constant.STATUS_SUCCESS) {
                        JSONObject collectObject = (JSONObject) response.getData();
                        if (collectObject.has("has_fav")) {
                            has_fav = collectObject.getBoolean("has_fav");
                        }
                    }
                    break;
                // 文评详情页
                case SweepFlowerService.HANDLE_TYPE_SWEEP_FLOWER:
                    if (DiaobaoUtil.isRequestResult(response)) {
                        SweepFlowerDesBean flowerBean = new SweepFlowerDesBean();
                        flowerBean.setJson2((JSONObject) response.getData());
                        initDate(flowerBean);
                        if (flowerBean.getFlower_users() != null && flowerBean.getFlower_users().length != 0) {
                            List<SweepFlowerBean> userBeanList = Arrays.asList(flowerBean.getFlower_users());
                            no_flower.setVisibility(View.GONE);
                            flowerUserAdapter.setFlowerInfo(userBeanList);
                            flowerSum = DiaobaoUtil.String2Int(flowerBean.getReview().getFlower_num());
                            flower_sum.setText(flowerSum + "");

                        } else {
                            no_flower.setVisibility(View.VISIBLE);
                            flower_sum.setText("0");
                        }
                    } else {
                        UIUtil.toastMessage(SweepFlowerActivity.this, DiaobaoUtil.getErrorTips(response));
                    }
                    break;
                default:
                    break;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    OnClickListener sweepFlowerClick = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.sweep_flower:
                    if (!AccountService.getInstance().isLogin()) {
                        AccountService.getInstance().jumpToLogin();
                        return;
                    }
                    if (clickable) {
                        // 定义计时器
                        if (!isFlowerClick) {
                            isFlowerClick = true;
                            flower.startAnimation(flowerAnimation);
                            flowerStatusTips.setVisibility(View.VISIBLE);
                            flowerIngUserImg.setVisibility(View.VISIBLE);
                            AlphaAnimation alpha = new AlphaAnimation(0f, 1f);
                            alpha.setDuration(1000);
                            alpha.setFillAfter(true);
                            flowerIngUserImg.startAnimation(alpha);
                            flowerStatusTips.startAnimation(alpha);
                            flower_copy[j % 8].setVisibility(View.VISIBLE);
                            ++flower_count;
                            ++count;
                            AnimationUtil.flowerAnimation(flower_copy[j % 8]);
                            j++;
                            timer = new Timer();
                            timer.schedule(new TimerTask() {
                                int i = 0;

                                @Override
                                public void run() {
                                    Message msg = Message.obtain();
                                    msg.what = i++;
                                    flowerHandler.sendMessage(msg);
                                }
                            }, 1000, 1000);
                        } else {
                            flower_copy[j % 8].setVisibility(View.VISIBLE);
                            // flower_sum.setText(++flower_count + "");
                            ++flower_count;
                            ++count;
                            AnimationUtil.flowerAnimation(flower_copy[j % 8]);
                            j++;
                        }
                        // startFlowerTips.setVisibility(View.INVISIBLE);
                        startFlowerTips.setText("请连续戳我");
                        flowerStatusTips.setText(flower_count + "");
                    }

                    break;
                case R.id.flower_sum:
                    Intent rankIntent = new Intent(SweepFlowerActivity.this, FlowerRankActivity.class);
                    rankIntent.putExtra(Constant.SWEEP_FLOWER_USER, String.valueOf(mReviewId));
                    startActivity(rankIntent);
                    break;
                default:
                    break;
            }
        }
    };

    public void topBack(View view) {
        onBackPressed();
    }

    private ImageView ivCollect;
    private TextView tvCollect;
    private PopupWindow mMorePopupWindow;
    private boolean has_fav = false;
    private LinearLayout mMangerLayout;
    private View mReportLine;

    private void makeMorePopupWindow() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mMoreView = inflater.inflate(R.layout.popup_reader_more_panel, null);
        View lay = mMoreView.findViewById(R.id.lay);
        lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMorePopupWindow != null) {
                    mMorePopupWindow.dismiss();
                }
            }
        });
        // 收藏
        LinearLayout mCollect = (LinearLayout) mMoreView.findViewById(R.id.popup_reader_more_collect);
        mCollect.setOnClickListener(morePopopOnclick);
        ivCollect = (ImageView) mMoreView.findViewById(R.id.popup_reader_more_iv_collect);
        tvCollect = (TextView) mMoreView.findViewById(R.id.popup_reader_more_tv_collect);
        // 举报
        LinearLayout mReport = (LinearLayout) mMoreView.findViewById(R.id.popup_reader_more_report);
        mReport.setOnClickListener(morePopopOnclick);
        mReport.setVisibility(View.VISIBLE);
        mReportLine = mMoreView.findViewById(R.id.popup_reader_more_report_line);
        // 送花
        LinearLayout mFlow = (LinearLayout) mMoreView.findViewById(R.id.popup_reader_more_flow_ruel);
        mFlow.setOnClickListener(morePopopOnclick);
        mFlow.setVisibility(View.VISIBLE);
        mMoreView.findViewById(R.id.popup_reader_more_flow_ruel_line).setVisibility(View.VISIBLE);
        // 管理员
        mMangerLayout = (LinearLayout) mMoreView.findViewById(R.id.popup_reader_more_manger);
        mMangerLayout.setOnClickListener(morePopopOnclick);

        mMorePopupWindow = new PopupWindow(mMoreView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mMorePopupWindow.setAnimationStyle(R.style.Animation_Right_Left);
        mMorePopupWindow.setBackgroundDrawable(new ColorDrawable());

    }

    public void showMorePopupWindow(View view) {
        try {
            if (mMorePopupWindow == null) {
                return;
            }
            // if (mSweepHomeBean != null) {
            // has_fav = mSweepHomeBean.getHas_fav();
            // }
            if (has_fav) {
                ivCollect.setImageResource(R.drawable.btn_reader_collect_pressed);
                tvCollect.setText("已收藏");
            } else {
                ivCollect.setImageResource(R.drawable.btn_reader_collect_normal);
                tvCollect.setText("收藏");
            }
            if ("1".equals(isAdministrator)) {
                mMangerLayout.setVisibility(View.VISIBLE);
                mReportLine.setVisibility(View.VISIBLE);
            }
            mMorePopupWindow.showAtLocation(getWindow().getDecorView(), Gravity.TOP | Gravity.RIGHT, ScreenUtil2.dip2px(4), 0);
            mMorePopupWindow.setFocusable(true);
            mMorePopupWindow.setOutsideTouchable(true);
            mMorePopupWindow.update();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private OnClickListener morePopopOnclick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mMorePopupWindow != null && mMorePopupWindow.isShowing()) {
                mMorePopupWindow.dismiss();
            }
            switch (v.getId()) {
                case R.id.popup_reader_more_collect:
                    if (AccountService.getInstance().isLogin()) {

                        if (has_fav) {
                            SweepFlowerService.getInstance().loadSweepRemoveCollect(mReviewId, SweepFlowerActivity.this);
                        } else {
                            SweepFlowerService.getInstance().loadSweepCollect(mReviewId, SweepFlowerActivity.this);

                        }
                        // if (mSweepHomeBean != null) {
                        // mSweepHomeBean.setHas_fav(!has_fav);
                        // }
                        if (has_fav) {
                            UIUtil.toastMessage(SweepFlowerActivity.this, "取消收藏");
                            has_fav = false;
                        } else {
                            UIUtil.toastMessage(SweepFlowerActivity.this, "收藏成功");
                            has_fav = true;
                        }
                    } else {
                        AccountService.getInstance().jumpToLogin();
                    }
                    break;
                case R.id.popup_reader_more_flow_ruel:
                    showFlowerRule(v);
                    break;
                case R.id.popup_reader_more_report:
                    if (mReviewId != null && userBean != null) {
                        HomeMainServices.reportContentRequest(SweepFlowerActivity.this, userBean.getUserId(), String.valueOf(Constant.OBJECT_TYPE_SWEEP), mReviewId);
                    }
                    break;
                case R.id.popup_reader_more_manger:
                    Intent intent = new Intent();
                    intent.setClass(SweepFlowerActivity.this, BrowseActivity.class);
                    intent.putExtra(Constant.URL, "http://api.doufu.la/index.php/management/novelReviews/mobileIndex?id=" + mReviewId);
                    intent.putExtra(Constant.TITLE, "管理");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    // }
                    break;
                default:
                    break;
            }
        }
    };

    private Handler flowerHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == (SEND_FLOWER_TIME)) {
                flowerStatusTips.setText("时间到!");
                startFlowerTips.setText("");
                clickable = false;
                isFlowerClick = false;
                flower.clearAnimation();
                if (timer != null) {
                    timer.cancel();
                    timer = null;
                }
                flowerHandler.sendMessageDelayed(flowerHandler.obtainMessage(REFRESH_FLOWER_STATUS), 500);
            } else if (msg.what == SEND_FLOWER_SUCCESS) {

                DiaobaoUtil.flowerFinish(flowerStatusTips, flower_count, "文评者");

                SpannableString textSpan = new SpannableString("扣5个豆子");
                ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#f96d6d"));
                try {
                    Pattern p = Pattern.compile("5");
                    Matcher m = p.matcher("扣5个豆子");
                    while (m.find()) {
                        int start = m.start();
                        int end = m.end();
                        textSpan.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                } catch (PatternSyntaxException e) {
                    e.printStackTrace();
                }
                // flowerStatusTips.setText(textSpan);
                flower_sum.setText("" + (flowerSum + flower_count));
                AnimationUtil.starAlphaAnimation(flowerIngUserImg, 3000);
                AnimationUtil.starAlphaAnimation(flowerStatusTips, 3000);
                flower_count = 0;
                count = 0;
                flowerHandler.sendMessageDelayed(flowerHandler.obtainMessage(RESET_CLICK_FLOWER), 4000);
            } else if (msg.what == SEND_FLOWER_FAIL) {
                flowerStatusTips.setText((String) msg.obj);
                flower_sum.setText("" + (flowerSum + flower_count - count));
                AnimationUtil.starAlphaAnimation(flowerIngUserImg, 1000);
                AnimationUtil.starAlphaAnimation(flowerStatusTips, 1000);
                flower_count = 0;
                count = 0;
                flowerHandler.sendMessageDelayed(flowerHandler.obtainMessage(RESET_CLICK_FLOWER), 4000);
            } else if (msg.what == REFRESH_FLOWER_STATUS) {// 献花结束，刷新
                /** 提交献花数量 */
                SweepFlowerService.getInstance().sendFlower(mReviewId, count + "", SweepFlowerActivity.this);
                /** 刷新排名 */
                SweepFlowerService.getInstance().loadFlowerRank(mReviewId, 0, Constant.PAGE_SIZE_6, "", SweepFlowerActivity.this);
            } else if (msg.what == RESET_CLICK_FLOWER) {// 恢复献花按钮
                clickable = true;
                flowerStatusTips.setText("");
                // startFlowerTips.setVisibility(View.VISIBLE);
                startFlowerTips.setText("赠人菊花，手有余香");
            }

        }

        ;
    };

    private void initDate(SweepFlowerDesBean desBean) {
        SweepBean sweepBean = desBean.getReview();
        userBean = sweepBean.getUser();
        NovelBean novelBean = sweepBean.getNovel();
        UIUtil.showStarLevel(Integer.valueOf(sweepBean.getStar()), mTitleLevel);
        sweepDes.setText(sweepBean.getReview());

        mUserName.setText(userBean.getName());
        detailRatingBar.setRating(Float.valueOf(sweepBean.getStar()));
        ImageManager.displayImage(mUserImg, userBean.getIcon(), 0);
        mUserImg.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String userId = userBean.getUserId();
                if (!userId.equals(AccountService.getInstance().getUserUid())) {
                    UserDetailActivity.Companion.startActivity(v.getContext(),userId);
                }

            }
        });
        mSweepCreateTime.setText(DiaobaoUtil.seconds2TimeString(Long.parseLong(sweepBean.getAdd_time())));
        DiaobaoUtil.setMedalImgView(userBean.getMedal(), medalImg);

        ImageManager.displayImage(flowerIngUserImg, userBean.getIcon(), 0);
        flower.setOnClickListener(sweepFlowerClick);
        flower_sum.setOnClickListener(sweepFlowerClick);
        sweepTitle.setText(novelBean.getTitle());
        isAdministrator = desBean.getIsAdministrator();
        has_fav = desBean.isHas_fav();
        if (sweepBean.getTag_list() != null && !TextUtil.isEmpty(sweepBean.getTag_list())) {
            handleTagsData(sweepBean.getTag_list());
        }

    }

    // 初始化标签栏
    private void initTagsView() {
        tagsDataList = new ArrayList<String>();

        tagsView = (ContentTagsViewGroup) header.findViewById(R.id.sweep_tags_layout);
        mSweepCreateTime = (TextView) header.findViewById(R.id.sweep_create_time);

    }

    // 展现标签栏数据
    private void handleTagsData(String tagsData) {
        String[] tagsArray = tagsData.split("\\s+");

        tagsView.setTextTags(this, tagsArray);
    }
}
